/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.dataanalysis;

import java.util.Map;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.client.Util.GwtRpcDataSource;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.DataAnalysisAction;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.DataAnalysisActionAsync;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class ServicesGrid extends ListGrid
{

	class ServicesDataSource extends GwtRpcDataSource
	{

		

		public ServicesDataSource()
		{
			super();
			DataSourceTextField idField = new DataSourceTextField("filekey");
			idField.setPrimaryKey(true);
			idField.setHidden(true);

			DataSourceTextField descriptionField = new DataSourceTextField("description");

			setFields(idField, descriptionField);
		}

		@Override
		protected void executeAdd(String requestId, DSRequest request, DSResponse response)
		{
			//this is not supported
			response.setStatus(RPCResponse.STATUS_FAILURE);
			processResponse(requestId, response);
		}

		@Override
		protected void executeFetch(final String requestId, final DSRequest request, final DSResponse response)
		{
			ApplicationCallBack<Map<String, String>> callback = new ApplicationCallBack<Map<String, String>>()
			{

				public void onFailureAfter(Throwable caught)
				{
					
					response.setStatus(RPCResponse.STATUS_FAILURE);
					processResponse(requestId, response);
					SC.warn(caught.getMessage(), StaticHolder.dumpB);
				}

				public void onSuccessAfter(Map<String, String> services)
				{

					ListGridRecord[] list = new ListGridRecord[services.size()];
					int count=0;
					for (Map.Entry<String, String> entry: services.entrySet())
					{
						ListGridRecord record = new ListGridRecord();
						record.setAttribute("filekey", entry.getKey());
						record.setAttribute("description", entry.getKey()+":"+entry.getValue());
						list[count] = record;
						count++;
					}
					
					response.setData(list);
					// IMPORTANT: for paging to work we have to specify size of full result set
					//response.setStartRow(request.getStartRow());
					//response.setEndRow(request.getStartRow()+list.length);
	                response.setTotalRows (services.size());
					processResponse(requestId, response);

				}

				public void execute()
				{

					DataAnalysisActionAsync proxy=GWT.create(DataAnalysisAction.class);
					proxy.getRelatedServices(this);
				}

			};
			callback.execute();

		}

		@Override
		protected void executeRemove(final String requestId, final DSRequest request, final DSResponse response)
		{
			//this is not supported
			response.setStatus(RPCResponse.STATUS_FAILURE);
			processResponse(requestId, response);
		}

		@Override
		protected void executeUpdate(String requestId, DSRequest request, DSResponse response)
		{
			//this is not supported
			response.setStatus(RPCResponse.STATUS_FAILURE);
			processResponse(requestId, response);

		}

	}

	
	public ServicesGrid()
	{
		setWidth(500);
		setHeight(400);
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setShowEmptyMessage(true);
		setEmptyMessage("No service is related to input types");
		setSelectionType(SelectionStyle.SINGLE);
		setShowHeader(false);
		setDataSource(new ServicesDataSource());
		ListGridField descField = new ListGridField("description", "Description", 500);
		descField.setType(ListGridFieldType.TEXT);

		
		setFields(descField);
	}

	boolean loaded = false;

	public void load()
	{
		if (loaded)
			return;
		fetchData();
		loaded = true;
	}

	public void reload()
	{
		invalidateCache();
		fetchData();
		loaded = true;
	}
}
